// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.editors.editmanager;

import org.dengues.designer.ui.database.figures.DBNoteFigure;
import org.dengues.designer.ui.database.parts.DBNoteEditPart;
import org.dengues.designer.ui.process.models.CompNote;
import org.dengues.model.database.DiagramChild;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.tools.CellEditorLocator;
import org.eclipse.gef.tools.DirectEditManager;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * @since 2007-9-22 下午09:44:42
 */
public class DiagramChildDirectEditManager extends DirectEditManager {

    /**
     * Qiang.Zhang.Adolf@gmail.com DBTableDirectEditManager constructor comment.
     * 
     * @param source
     * @param editorType
     * @param locator
     */
    public DiagramChildDirectEditManager(GraphicalEditPart source, Class editorType, CellEditorLocator locator) {
        super(source, editorType, locator);
    }

    @Override
    protected CellEditor createCellEditorOn(Composite composite) {
        return new TextCellEditor(composite, SWT.MULTI | SWT.WRAP);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.tools.DirectEditManager#initCellEditor()
     */
    @Override
    protected void initCellEditor() {
        String name = "";
        if (getEditPart() instanceof DBNoteEditPart) {
            DBNoteFigure noteFigure = (DBNoteFigure) getEditPart().getFigure();
            name = noteFigure.getText();
        } else if (getEditPart().getModel() instanceof CompNote) {
            name = ((CompNote) getEditPart().getModel()).getText();
        } else {
            name = ((DiagramChild) getEditPart().getModel()).getName();
        }
        getCellEditor().setValue(name);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.tools.DirectEditManager#show()
     */
    @Override
    public void show() {
        super.show();
        if (getCellEditor().getControl() instanceof Text) {
            Text text = (Text) getCellEditor().getControl();
            text.setSelection(text.getText().length(), text.getText().length());
        }
    }

}
